package com.sky.happygos.present;

import android.util.Log;

import com.sky.happygos.event.ChartOrderDataE;
import com.sky.happygos.event.ChartOrderSimpleE;
import com.sky.happygos.event.FailOrderE;
import com.sky.happygos.event.FinishOrderE;
import com.sky.happygos.event.GoodsListE;
import com.sky.happygos.event.OrderStatusOneE;


import com.sky.happygos.event.ReceiveOrderE;
import com.sky.happygos.model.ChartSimple;
import com.sky.happygos.model.Goods;
import com.sky.happygos.model.ID;
import com.sky.happygos.model.Order;
import com.sky.happygos.model.RestMessage;
import com.sky.happygos.network.Api;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.greenrobot.eventbus.EventBus;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static org.androidannotations.annotations.EBean.Scope.Singleton;

/**
 * Created by qqqqqqq on 2017/4/18.
 */
@EBean(scope = Singleton)
public class OrderP {

    @Bean
    Api api;

    @Background
    public void getOrders(final int status){
        Call<List<Order>> call = api.getAuthService().listOrder(status);
        call.enqueue(new Callback<List<Order>>() {
            @Override
            public void onResponse(Call<List<Order>> call, Response<List<Order>> response) {
                if(response.code() == 200){
                    List<Order> orders = response.body();
                    EventBus.getDefault().post(new OrderStatusOneE(orders,true,status));
                }else{
                    EventBus.getDefault().post(new OrderStatusOneE(response,status));
                }
                Log.v("goods:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<List<Order>> call, Throwable t) {
                EventBus.getDefault().post(new OrderStatusOneE(null,status));
            }
        });
    }

    @Background
    public void finishOrder(String orderId,final int status){
        Call<RestMessage> call = api.getAuthService().finishOrder(new ID(orderId));
        call.enqueue(new Callback<RestMessage>() {
            @Override
            public void onResponse(Call<RestMessage> call, Response<RestMessage> response) {
                if(response.code() == 200){
                    RestMessage message = response.body();
                    EventBus.getDefault().post(new FinishOrderE(message,true,status));
                }else{
                    EventBus.getDefault().post(new FinishOrderE(response,status));
                }
                Log.v("finishOrder:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<RestMessage> call, Throwable t) {
                EventBus.getDefault().post(new FinishOrderE(null,status));
            }
        });
    }

    @Background
    public void failOrder(String orderId,final int status){
        Call<RestMessage> call = api.getAuthService().failOrder(new ID(orderId));
        call.enqueue(new Callback<RestMessage>() {
            @Override
            public void onResponse(Call<RestMessage> call, Response<RestMessage> response) {
                if(response.code() == 200){
                    RestMessage message = response.body();
                    EventBus.getDefault().post(new FailOrderE(message,true,status));
                }else{
                    EventBus.getDefault().post(new FailOrderE(response,status));
                }
                Log.v("finishOrder:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<RestMessage> call, Throwable t) {
                EventBus.getDefault().post(new FailOrderE(null,status));
            }
        });
    }

    @Background
    public void receiveOrder(String orderId,final int status){
        Call<RestMessage> call = api.getAuthService().receiveOrder(new ID(orderId));
        call.enqueue(new Callback<RestMessage>() {
            @Override
            public void onResponse(Call<RestMessage> call, Response<RestMessage> response) {
                if(response.code() == 200){
                    RestMessage message = response.body();
                    EventBus.getDefault().post(new ReceiveOrderE(message,true,status));
                }else{
                    EventBus.getDefault().post(new ReceiveOrderE(response,status));
                }
                Log.v("finishOrder:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<RestMessage> call, Throwable t) {
                EventBus.getDefault().post(new ReceiveOrderE(null,status));
            }
        });
    }


    @Background
    public void getChartData(final int status){
        Call<List<Order>> call;
        if(status == 0){
            call = api.getAuthService().getMonthData();
        }else{
            call = api.getAuthService().getYearData();
        }

        call.enqueue(new Callback<List<Order>>() {
            @Override
            public void onResponse(Call<List<Order>> call, Response<List<Order>> response) {
                if(response.code() == 200){
                    List<Order> orders = response.body();
                    EventBus.getDefault().post(new ChartOrderDataE(orders,true,status));
                }else{
                    EventBus.getDefault().post(new ChartOrderDataE(response,status));
                }
                Log.v("goods:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<List<Order>> call, Throwable t) {
                EventBus.getDefault().post(new ChartOrderDataE(null,status));
            }
        });
    }

    @Background
    public void getChartSimple(final int status){

        Call<ChartSimple> call;
        if(status == 0) {
            call = api.getAuthService().getTodayChart();
        }else{
            call = api.getAuthService().getMonthChart();
        }
        call.enqueue(new Callback<ChartSimple>() {
            @Override
            public void onResponse(Call<ChartSimple> call, Response<ChartSimple> response) {
                if(response.code() == 200){
                    ChartSimple message = response.body();
                    EventBus.getDefault().post(new ChartOrderSimpleE(message,true,status));
                }else{
                    EventBus.getDefault().post(new ChartOrderSimpleE(response,status));
                }
                Log.v("finishOrder:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<ChartSimple> call, Throwable t) {
                EventBus.getDefault().post(new ChartOrderSimpleE(null,status));
            }
        });
    }



}
